<?php
/**
 * Prosta klasa do obslugi danych wejsciowych (np _POST lub _GET), pobiera rowniez
 * dane z REQUEST_URI (mod_rewrite). Umozliwia wstepna walidacje danych.
 * @author Miron Pawlik 
 * 
 */

define('FD_STRING', 1);
define('FD_INT', 2);

class FrontData implements ArrayAccess
{

	/**
	 * Zawiera wszsytkie zapisane informacje
	 * @var array
	 * @static
	 * @access private
	 */
	static private $params = array();
	
	/**
	 * Konstruktor, pobiera dane z adresu i z posta
	 * dodatkowo parsuje dane zawarte w sciezce dostepu (REQUEST_URI)
	 * 
	 * @param string $string additional input data
	 */
	public function __construct(Config $config = null)
	{
		$arr = array();
		$arr = explode(',', $_SERVER["QUERY_STRING"]);
		$arr += $_GET;
		$arr += $_POST;
		
		
		$this->params = $arr;
		if(get_magic_quotes_gpc())
			$this->StripArray($this->params);
	}
	
	/**
	 * getting one param
	 * 
	 * @param string $name name of param
	 * @param int $type type of param
	 * @return string
	 */
	public function getParam( $name , $type = 1)
	{
		switch($type)
		{
			case 2:
				if(is_numeric( $this -> params[$name]) and $this -> params[$name] >= 0 )
					return $this -> params[$name];
				else
					return 0;
			default:
				return $this -> params[$name];
		}
	}
  
	/**
	 * checking is parametr set
	 * 
	 * @param string $name
	 * @return boolean
	 */
	public function issetParam( $name )
	{
		return isset($this -> params[$name]);
	}

	/**
	 * checking is parametr empty
	 * 
	 * @param string $name
	 * @return boolean
	 */
	public function emptyParam( $name )
	{
		return empty($this -> params[$name]);
	}
	
	/**
	 * if param $name value is in $case array return $name value, else return $default value
	 * 
	 * @param string $name
	 * @param array $case
	 * @param mixed $default
	 * @return mixed
	 */
	public function getCaseParam( $name , array $case , $default = false)
	{
		if( in_array($this -> params[$name], $case) )
			return $this -> params[$name];
		else
			return $default;
	}

	/**
	 * update value param $name with $newValue
	 * 
	 * @param string $name
	 * @param mixed $newValue
	 */
	private function updateParam( $name , $newValue )
	{
		$this -> params[$name] = $newValue;
		return true;
	}
  
	/**
	 * return array where keys are values from $args, and values are from $params
	 * 
	 * @param array &$args
	 */
	public function postToArg( array &$args )
	{
		$arr = array();
		foreach( $args as $v)
			$arr[$v] =  $this -> params[$v];
		return $arr;
	}
  
	/**
	 *  Entities all submitted data
	 */
	public function postEntities($var = 0)
	{
		if(!$var)
			$var = &$this -> params;
		foreach ($var as $key=>$value)
			if (is_array($var[$key]))
				$this -> postEntities($var[$key]);
			else
				$var[$key] = htmlentities($value, ENT_QUOTES, 'UTF-8');
	}
  
	/**
	 * Exploding param $name, using $delimiter
	 *
	 * @param string $name
	 * @param string $delimiter
	 * @return array
	 */
	public function explodeParam($name, $delimiter = ',' )
	{
		return explode($delimiter, $this->param[$name]);
	}
	
	/**
	 * Funkcje interfejsu ArrayAccess
	 */
	
	public function offsetExists($offset)
	{
		return isset($this->params[$offset]);
	}
 	public function offsetGet ($offset)
	{
		if(isset($this->params[$offset]))
		{
			return $this->params[$offset];
		}
		else
		{
			return false;
		}
	}
 	public function offsetSet ($offset, $value)
	{
		$this->params[$offset] = $value;
	}
 	public function offsetUnset ($offset)
	{
		unset($this->params[$offset]);
	}
	
	/**
	 * dump all input values
	 */
	public function vardump()
	{	
		return var_dump($this -> params);
	}
  
	/**
	 * strip array
	 * @access protected
	 * @param array &$var
	 */
	protected function StripArray( array &$var )
	{
		foreach ($var as $key=>$value)
			if (is_array($var[$key]))
				$this -> StripArray($var[$key]);
			else
				$var[$key] = stripslashes($value);
	}
	
	public function convert2link(dbHandler $db, $field, $table, $uniqueField, $cond = '')
	{
		$iso = array('Ę'=>'E','Ó'=>'O','Ą'=>'A','Ś'=>'S','Ł'=>'L','Ż'=>'Z','Ź'=>'Z','Ć'=>'Z','Ń'=>'N',
					'ę'=>'e','ó'=>'o','ą'=>'a','ś'=>'s','ł'=>'l','ż'=>'z','ź'=>'z','ć'=>'c','ń'=>'n');
		$noniso = array('E','O','A','S','L','Z','Z','C','N','e','o','a','s','l','z','z','c','n');
		$arr = str_split( trim(strtolower(strtr($this[$field], $iso) )));
		//$arr = str_split($this[$field]);
		
		$link = '';
		foreach( $arr as $ch )
			if( (ord($ch) < 123 and ord($ch) > 96) or  (ord($ch) < 58 and ord($ch) > 47) or $ch == '-' or $ch == '_' or $ch == '+')
				$link.=$ch;
			else
				$link.='-';
		$link = trim($link, '-');
		$sufix = '';
		if($cond != '')
			$cond = ' AND '.$cond;
		while( $db->countRec($table, $uniqueField.' = \''.$link.$sufix.'\''.$cond) )
		{
			$sufix++;
		}

		return $link.$sufix;
			
	}
}
